package com.zk.service.impl;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zk.config.AliPayConfig;
import com.zk.dao.UserDao;
import com.zk.model.Goods;
import com.zk.model.User;
import com.zk.service.AlipayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//调用支付宝接口
@Service
public class AlipayServiceImpl implements AlipayService {
    @Resource
    private UserDao userDao;
    @Transactional
    @Override
    public void aliPay(HttpServletResponse response, HttpServletRequest request, HttpSession session) throws IOException {
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl("http://127.0.0.1:9001/gwsc/succes");
        aliPayRequest.setNotifyUrl("http://127.0.0.1:9001/gwsc");
        Goods goods=(Goods) session.getAttribute("id");
        //商户订单号，后台可以写一个工具类生成一个订单号，必填
        String order_number = request.getParameter("order_number");
        //付款金额，从前台获取，必填
        String total_amount=request.getParameter("total_amount");
        //订单名称，必填
        String subject=goods.getName();
        String sum=request.getParameter("subject");
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        //保存订单
        User user=(User) session.getAttribute("user");

        Integer goodId=goods.getId();
        Integer userId=user.getId();
        Integer serial=userDao.selectSerialByGoodsId(goodId);
        Integer stock=userDao.selectGoodsNum(goodId)-Integer.valueOf(sum);
        if (stock<0){
            int i=0/10;
        }
        userDao.updateStock(goodId,stock);
        Integer serial2=userDao.selectSerialByGoodsId(goodId);
        if (serial!=serial2){
            int i=0/10;
        }
        serial2=serial2+1;
        int nn=userDao.updateSerial(goodId,serial2);
        int num=userDao.addOrder(goodId,userId,order_number,Integer.valueOf(sum),Integer.valueOf(total_amount));
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        out.println(result);
    }
}
